package com.labyrinth.core;

public class MovableBrick extends Brick
{
	private float radius;// This will be used to create a circle within the
							// brick that will detect the touch of the player

	public MovableBrick(float x, float y, float height, float width, int inDirection)
	{
		super(x, y, height, width, inDirection);
		setTexture("data/brick.jpg");
		radius = getScaleY() / 2;
	}

	// Check whether the given point is within the inner circle of the brick
	public boolean checkInnerCircle(Vector2D origin)
	{
		float centerX = position.getX();
		float centerY = position.getY();

		if (origin != null && (Math.pow(centerX - origin.getX(), 2) + Math.pow(centerY - origin.getY(), 2)) <= Math.pow(radius, 2))
		{
			return true;
		}
		return false;
	}

	MovableBrick getNewMovableBrick()
	{
		return new MovableBrick(this.getX(), this.getY(), this.getScaleY(), this.getScaleX(), this.getDirection());
	}

	// Takes in the starting position of the direction vector and its end point, then it will move the brick accordingly (If the player moves the brick so x==y then nothing happens)
	public boolean moveBrick(Vector2D start, Vector2D end, GameObject[][] inLevel, int row, int column, Player player)
	{
		MovableBrick tmp = null;
		boolean moved = false;
		int playerx = (int)(player.getX()/this.scaleX);
		int playery = (int)(player.getY()/this.scaleY);
		
		if (end.getX() - start.getX() >= 0)
		{
			if (end.getY() - start.getY() >= 0)
			{
				if (Math.abs(end.getX() - start.getX()) > Math.abs(end.getY() - start.getY()))// Right
				{
					if (column + 1 < inLevel[0].length && inLevel[row][column + 1].getClass().equals(Floor.class) && !(row==playery && column+1==playerx))
					{
						tmp = ((MovableBrick) inLevel[row][column]).getNewMovableBrick();
						tmp.setPosition(tmp.getX() + tmp.getScaleX(), tmp.getY());
						inLevel[row][column + 1] = tmp;
						inLevel[row][column] = new Floor(inLevel[row][column].getX(), inLevel[row][column].getY(), inLevel[row][column].getScaleY(), inLevel[row][column].getScaleX());
						moved=true;
					}
				}
				else if (Math.abs(end.getX() - start.getX()) < Math.abs(end.getY() - start.getY()))// Up
				{
					if (row + 1 >= 0 && row + 1 < inLevel.length && inLevel[row + 1][column].getClass().equals(Floor.class) && !(row+1==playery && column==playerx))
					{
						tmp = ((MovableBrick) inLevel[row][column]).getNewMovableBrick();
						tmp.setPosition(tmp.getX(), tmp.getY() + tmp.getScaleY());
						inLevel[row][column] = new Floor(inLevel[row][column].getX(), inLevel[row][column].getY(), inLevel[row][column].getScaleY(), inLevel[row][column].getScaleX());
						inLevel[row + 1][column] = tmp;
						moved=true;
					}
				}
			}
			else
			{
				if (Math.abs(end.getX() - start.getX()) > Math.abs(end.getY() - start.getY()))// Right
				{
					if (column + 1 < inLevel[0].length && inLevel[row][column + 1].getClass().equals(Floor.class) && !(row==playery && column+1==playerx))
					{
						tmp = ((MovableBrick) inLevel[row][column]).getNewMovableBrick();
						tmp.setPosition(tmp.getX() + tmp.getScaleX(), tmp.getY());
						inLevel[row][column] = new Floor(inLevel[row][column].getX(), inLevel[row][column].getY(), inLevel[row][column].getScaleY(), inLevel[row][column].getScaleX());
						inLevel[row][column + 1] = tmp;
						moved=true;
					}
				}
				else if (Math.abs(end.getX() - start.getX()) < Math.abs(end.getY() - start.getY()))// Down
				{
					if (row - 1 >= 0 && inLevel[row - 1][column].getClass().equals(Floor.class) && !(row-1==playery && column==playerx))
					{
						tmp = ((MovableBrick) inLevel[row][column]).getNewMovableBrick();
						tmp.setPosition(tmp.getX(), tmp.getY() - tmp.getScaleY());
						inLevel[row][column] = new Floor(inLevel[row][column].getX(), inLevel[row][column].getY(), inLevel[row][column].getScaleY(), inLevel[row][column].getScaleX());
						inLevel[row - 1][column] = tmp;
						moved=true;
					}
				}
			}
		}
		else
		{
			if (end.getY() - start.getY() >= 0)
			{
				if (Math.abs(end.getX() - start.getX()) > Math.abs(end.getY() - start.getY()))// Left
				{
					if (column - 1 >= 0 && inLevel[row][column - 1].getClass().equals(Floor.class) && !(row==playery && column-1==playerx))
					{
						tmp = ((MovableBrick) inLevel[row][column]).getNewMovableBrick();
						tmp.setPosition(tmp.getX() - tmp.getScaleX(), tmp.getY());
						inLevel[row][column] = new Floor(inLevel[row][column].getX(), inLevel[row][column].getY(), inLevel[row][column].getScaleY(), inLevel[row][column].getScaleX());
						inLevel[row][column - 1] = tmp;
						moved=true;
					}
				}
				else if (Math.abs(end.getX() - start.getX()) < Math.abs(end.getY() - start.getY()))// Up
				{
					if (row + 1 >= 0 && row + 1 < inLevel.length && inLevel[row + 1][column].getClass().equals(Floor.class)&& !(row+1==playery && column==playerx)) 
					{
						tmp = ((MovableBrick) inLevel[row][column]).getNewMovableBrick();
						tmp.setPosition(tmp.getX(), tmp.getY() + tmp.getScaleY());
						inLevel[row][column] = new Floor(inLevel[row][column].getX(), inLevel[row][column].getY(), inLevel[row][column].getScaleY(), inLevel[row][column].getScaleX());
						inLevel[row + 1][column] = tmp;
						moved=true;
					}
				}
			}
			else
			{
				if (Math.abs(end.getX() - start.getX()) > Math.abs(end.getY() - start.getY()))// Left
				{
					if (column - 1 >= 0 && inLevel[row][column - 1].getClass().equals(Floor.class) && !(row==playery && column-1==playerx))
					{
						tmp = ((MovableBrick) inLevel[row][column]).getNewMovableBrick();
						tmp.setPosition(tmp.getX() - tmp.getScaleX(), tmp.getY());
						inLevel[row][column] = new Floor(inLevel[row][column].getX(), inLevel[row][column].getY(), inLevel[row][column].getScaleY(), inLevel[row][column].getScaleX());
						inLevel[row][column - 1] = tmp;
						moved=true;
					}
				}
				else if (Math.abs(end.getX() - start.getX()) < Math.abs(end.getY() - start.getY()))// Down
				{
					if (row - 1 >= 0 && inLevel[row - 1][column].getClass().equals(Floor.class) && !(row-1==playery && column==playerx))
					{
						tmp = ((MovableBrick) inLevel[row][column]).getNewMovableBrick();
						tmp.setPosition(tmp.getX(), tmp.getY() - tmp.getScaleY());
						inLevel[row][column] = new Floor(inLevel[row][column].getX(), inLevel[row][column].getY(), inLevel[row][column].getScaleY(), inLevel[row][column].getScaleX());
						inLevel[row - 1][column] = tmp;
						moved=true;
					}
				}
			}
		}
		return moved;
	}
}
